unit Figure;

{
  PasCalc
  Version 1.2 2011-05-26

  Copyright (C) 2011 KEEPER <yar.keeper@gmail.com>

  This file is part of the PasCalc Calculator

  This unit is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See the file LICENSE.TXT, included in this distribution,
  for details about the copyright.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, StdCtrls, Menus, Options, Analyser, UsedTypes, GBuilder, CreateTable,
  NumerousMethods;

type

  { TfrmGraph }

  TfrmGraph = class(TForm)
    mnuBuildFromTable: TMenuItem;
    miDecr: TMenuItem;
    miInc: TMenuItem;
    mnuBuild: TMenuItem;
    mnuGraph: TMainMenu;
    miSave: TMenuItem;
    miClear: TMenuItem;
    dlgGraphSave: TSaveDialog;
    isGraph: TPaintBox;
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure isGraphMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure isGraphPaint(Sender: TObject);
    procedure miClearClick(Sender: TObject);
    procedure miDecrClick(Sender: TObject);
    procedure miIncClick(Sender: TObject);
    procedure miSaveClick(Sender: TObject);
    procedure mnuBuildClick(Sender: TObject);
    procedure mnuBuildFromTableClick(Sender: TObject);
  private
  public
    FuncValues: TFunction;
    procedure LangInit(str: TStringList);
  end; 

var
  frmGraph: TfrmGraph;
  edExpr: TEdit;

 {$i consts.inc}

implementation

{ TfrmGraph }

var
  CrossHair: TCrossHair;
  Fig: TFigure;

procedure TfrmGraph.FormShow(Sender: TObject);
var
  Pos: USedTypes.TPoint;
begin
  Pos.x := isGraph.Width div 2;
  Pos.y := isGraph.Height div 2;
  CrossHair := TCrossHair.Create(isGraph.Canvas, Pos);
  CrossHair.LinesColor := fOptions.NetColor;
  CrossHair.LinesWidth := DEF_LINES_WIDTH;
  CrossHair.OneSize := MIN_CELL_SIZE;
  CrossHair.Draw;
  Fig := TFigure.Create(isGraph.Canvas, Pos);

end;

procedure TfrmGraph.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  CrossHair.Free;
  Fig.Free;
end;

procedure TfrmGraph.isGraphMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  Pos: TPoint;
begin
  Pos.x := X;
  Pos.y := Y;
  if Fig.IsExists then exit;
  Crosshair.CursorPos := Pos;
  CrossHair.Redraw;
end;

procedure TfrmGraph.isGraphPaint(Sender: TObject);
begin
  if not Fig.IsExists then Crosshair.Redraw else
    Fig.Redraw;
end;

procedure TfrmGraph.miClearClick(Sender: TObject);
begin
  Fig.Delete;
  Crosshair.Redraw;
end;

var
  Size: integer = MIN_CELL_SIZE;

procedure TfrmGraph.miDecrClick(Sender: TObject);
begin
  if Fig.IsExists then
  begin
    if size > 2 then size := size - INC_CELL_SIZE;
    Fig.OneSize := Size;
    Fig.Redraw;
  end;
  Fig.SaveToFile(ANSITOUTF8(SavePath) + 'temp.bmp');
end;

procedure TfrmGraph.miIncClick(Sender: TObject);
begin
  if Fig.IsExists then
  begin
    size := size + INC_CELL_SIZE;
    Fig.OneSize := Size;
    Fig.Redraw;
  end;
  Fig.SaveToFile(ANSITOUTF8(SavePath) + 'temp.bmp');
end;

procedure TfrmGraph.miSaveClick(Sender: TObject);
begin
  if dlgGraphSave.Execute then
    case dlgGraphSave.FilterIndex of
      1: Fig.SaveToPng(dlgGraphSave.FileName);
      2: Fig.SaveToJpg(dlgGraphSave.FileName);
      3: CopyFile(ANSITOUTF8(SavePath) + 'temp.bmp', dlgGraphSave.FileName, false);
    end;
end;

procedure TfrmGraph.mnuBuildClick(Sender: TObject);
begin
  Size := MIN_CELL_SIZE;
  if edExpr.Text = '' then exit;
  Fig.OneSize := MIN_CELL_SIZE;
  Fig.GridStyle := fOptions.GraphStyle;
  Fig.CursorPos := Crosshair.CursorPos;
  Fig.LinesColor := fOptions.NetColor;
  Fig.ManyInOne := fOptions.ManyInOne;
  Fig.GraphColor := fOptions.GraphColor;
  Fig.LinesWidth := DEF_LINES_WIDTH;
  Fig.IsRad := true;
  Fig.Func := edExpr.Text;
  Fig.Draw;
  Fig.SaveToFile(ANSITOUTF8(SavePath) + 'temp.bmp');
end;

procedure TfrmGraph.mnuBuildFromTableClick(Sender: TObject);
begin
  Size := MIN_CELL_SIZE;
  frmCreateTable.ShowModal;
  if frmCreateTable.fBuilded then
  begin
    Fig.OneSize := MIN_CELL_SIZE;
    Fig.GridStyle := fOptions.GraphStyle;
    Fig.CursorPos := Crosshair.CursorPos;
    Fig.LinesColor := fOptions.NetColor;
    Fig.ManyInOne := fOptions.ManyInOne;
    Fig.GraphColor := fOptions.GraphColor;
    Fig.LinesWidth := DEF_LINES_WIDTH;
    Fig.IsRad := true;
    Fig.SourceTable := FuncValues;
    Fig.TableDraw;
    Fig.SaveToFile(ANSITOUTF8(SavePath) + 'temp.bmp');
  end;
end;

procedure TfrmGraph.LangInit(str: TStringList);
begin
  frmGraph.Caption := str[56];
  miSave.Caption := str[57];
  miClear.Caption := str[58];
  miDecr.Caption := str[59];
  miInc.Caption := str[60];
  mnuBuild.Caption := str[61];
  mnuBuildFromTable.Caption := str[62]
end;

initialization
  {$I Figure.lrs}

end.

